package demo.zyy.com.dreamstudio.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

/**
 * Created by Administrator on 2017/10/27.
 */

public class DeletEditText extends EditText {
    private Drawable mRinghtDrawable;
    boolean isHasFocus;
    private boolean clearDrawableVisible;

    //构造方法1
    public DeletEditText(Context context) {
        super(context);
        init();
    }

    public DeletEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DeletEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Drawable drawable[] = this.getCompoundDrawables();//本地获取空进啊上下左右四个方位的图片
        mRinghtDrawable = drawable[2];
        this.addTextChangedListener(new TextWatcherImpl());
        this.setOnFocusChangeListener(new OnFocusChangeImpl());
        setClearDrawableVisible(false);//初始设置所有右边图片不可见

    }

    public void setClearDrawableVisible(boolean isNoNull) {
        Drawable rightDrawable;
        if (isNoNull) {
            rightDrawable = mRinghtDrawable;
        } else {
            rightDrawable = null;
        }
        //使用代码设置该控件left,top，right,and bottom处的图标
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],rightDrawable, getCompoundDrawables()[3]);
    }

    class TextWatcherImpl implements TextWatcher {
        //下面是三个要覆写的方法
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            boolean isNoNull = getText().toString().length() >= 1;
            setClearDrawableVisible(isNoNull);
        }
    }

    class OnFocusChangeImpl implements OnFocusChangeListener {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            isHasFocus = hasFocus;
            if (isHasFocus) {//如果获取焦点
                boolean isNoNull = getText().toString().length() >= 1;
                setClearDrawableVisible(isNoNull);
            } else {
                setClearDrawableVisible(false);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
         switch (event.getAction()) {
             case MotionEvent.ACTION_UP:
                 int length1=getWidth()-getPaddingRight();//删除图片右侧到edittext控件最左侧距离
                 int length2=getWidth()-getTotalPaddingRight();//删除图片左侧到Edittext控件最左侧的距离
                 boolean isClean=(event.getX()>length2)&&(event.getX()<length1);
                 if(isClean){
                     setText("");
                 }
                 break;
         }
        return super.onTouchEvent(event);
    }
}
